package com.yuhang.security.controller.async;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class QueueListener implements ApplicationListener<ContextRefreshedEvent>{

	@Autowired
	private MockQueue mockQueue;
	
	@Autowired
	private DeferredResultHolder deferredResultHolder;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		new Thread(()->{
			while (true) {

				if (mockQueue.getCompleteOrder()!=null && !mockQueue.getCompleteOrder().equals("")) {
					
					String orderNUmber = mockQueue.getCompleteOrder();
					
					System.out.println("返回订单处理结果"+orderNUmber);
					
					deferredResultHolder.getMap().get(orderNUmber).setResult("place order success");
					
					mockQueue.setCompleteOrder(null);
				}else {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();;
		
		
	}
}
